package ducna.android.aoeclip.screen.category.loader;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import ducna.android.aoeclip.common.model.business.JSONParser;
import ducna.android.aoeclip.common.model.entity.Article;
import ducna.android.aoeclip.common.utils.Constant;
import ducna.android.aoeclip.common.view.AlertDialogManager;

/**
 * Created by DUCNA on 11/4/2014.
 */
public class DataArticleLoader extends AsyncTaskLoader<ArrayList<Article>> implements Constant {

    private ArrayList<Article> mResult;
    private String categoryId;

    public DataArticleLoader(Context context, String categoryId) {
        super(context);
        this.categoryId = categoryId;
    }

    @Override
    public ArrayList<Article> loadInBackground() {
        ArrayList<Article> dataArticles = new ArrayList<Article>();

        String url = String.format(URL_ARTICLE_LIST, categoryId);
        JSONObject articlesJson = JSONParser.getJSONFromUrl(url);
        if (articlesJson != null) {
            try {
                JSONArray arrServices = articlesJson.getJSONArray(KEY_ROOT);
                for (int i = 0; i < arrServices.length(); i++) {
                    JSONObject objArticle = arrServices.getJSONObject(i);
                    String id = objArticle.getString(KEY_ID);
                    String cateid = objArticle.getString(KEY_CATEID);
                    String duration = objArticle.getString(KEY_VIDEO_DURATION);
                    String image = objArticle.getString(KEY_VIDEO_IMAGE);
                    String link = objArticle.getString(KEY_VIDEO_LINK);
                    String name = objArticle.getString(KEY_VIDEO_NAME);
                    dataArticles.add(new Article(id, cateid, image, name,
                            link, duration));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return dataArticles;
    }

    @Override
    public void deliverResult(ArrayList<Article> data) {
        if (isReset()) {
            if (data != null)
                onReleaseResources(data);
        }
        ArrayList<Article> oldData = data;
        mResult = data;
        if (isStarted()){
            super.deliverResult(data);
        }

        if (oldData != null)
            onReleaseResources(oldData);

    }

    @Override
    protected void onStartLoading() {
        if (mResult != null)
            deliverResult(mResult);
        if (takeContentChanged() || mResult == null)
            forceLoad();
    }

    @Override
    public void stopLoading() {
        super.stopLoading();
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();
        onStopLoading();
        if (mResult != null) {
            onReleaseResources(mResult);
            mResult = null;
        }
    }

    @Override
    public void onCanceled(ArrayList<Article> data) {
        super.onCanceled(data);
        onReleaseResources(data);
    }

    protected void onReleaseResources(ArrayList<Article> data) {
        // For a simple List<> there is nothing to do. For something
        // like a Cursor, we would close it here.
    }
}
